//
//  AppDelegate.m
//  Feel_at_home
//
//  Created by dlios on 15/9/23.
//  Copyright © 2015年 Feel_at_home. All rights reserved.
//

#import "AppDelegate.h"
#import "WidthAndHeight.pch"
#import "HomeViewController.h"
#import "SingleViewController.h"
#import "ClassViewController.h"
#import "MineViewController.h"
#import "MusicViewController.h"
#import "Constants.h"
#import "UMSocial.h"

@interface AppDelegate ()<UIScrollViewDelegate, UITabBarControllerDelegate>

@property (nonatomic, strong) UITabBarController *tabbar;
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, strong) UIView *view;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    [UMSocialData setAppKey:@"55a3a50767e58eded90035da"];


    // tabBar铺设
    // 家居tabBar铺设
    HomeViewController *home = [[HomeViewController alloc] init];
    UINavigationController *homeVC = [[UINavigationController alloc] initWithRootViewController:home];
    [homeVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"heard"] forBarMetrics:UIBarMetricsDefault];
    homeVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:kHome image:[UIImage imageNamed:kHomeImage] tag:1001];
    homeVC.navigationBar.translucent = NO;
    
    // 单品tabBar铺设
    SingleViewController *single = [[SingleViewController alloc] init];
    UINavigationController *singleVC = [[UINavigationController alloc] initWithRootViewController:single];
    [singleVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"heard"] forBarMetrics:UIBarMetricsDefault];
    singleVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:kSingle image:[UIImage imageNamed:kSingleImage] tag:1002];
    singleVC.navigationBar.translucent = NO;
    
    // 分类tabBar铺设
    ClassViewController *class = [[ClassViewController alloc] init];
    UINavigationController *classVC = [[UINavigationController alloc] initWithRootViewController:class];
    [classVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"heard"] forBarMetrics:UIBarMetricsDefault];
    classVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:kClass image:[UIImage imageNamed:kClassImage] tag:1003];
    classVC.navigationBar.translucent = NO;
    
    // 我的tabBar铺设
    MineViewController *mine = [[MineViewController alloc] init];
    UINavigationController *mineVC = [[UINavigationController alloc] initWithRootViewController:mine];
    [mineVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"heard"] forBarMetrics:UIBarMetricsDefault];
    mineVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:kMine image:[UIImage imageNamed:kMineImage] tag:1004];
    mineVC.navigationBar.translucent = NO;
    
    // 心情tabBar铺设
    MusicViewController *music = [[MusicViewController alloc] init];
    UINavigationController *musicVC = [[UINavigationController alloc] initWithRootViewController:music];
    [musicVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"heard"] forBarMetrics:UIBarMetricsDefault];
    musicVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:kHappy image:[UIImage imageNamed:kHappyImage] tag:1005];
    musicVC.navigationBar.translucent = NO;
    
    // tabBar的设置
    self.tabbar = [[UITabBarController alloc] init];
    self.tabbar.viewControllers = [NSArray arrayWithObjects:homeVC, singleVC, musicVC, classVC, mineVC, nil];
    self.tabbar.tabBar.barTintColor = [UIColor whiteColor];
    /* 设置选中bar的颜色 */
    [[UITabBar appearance]setTintColor:[UIColor colorWithRed:13 / 255.0 green:184 / 255.0 blue:222 / 255.0 alpha:1]];
    /* 设置选中图标时显示的图片: */
    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:kElectImage]];
    
    // 预防引导页出现crash现象
    UIViewController* myvc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    self.window.rootViewController = myvc;
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        
        
        /* scrollView处理 */
        UIScrollView *myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375 * WIDTH, 667 * HEIGH)];
        myScroll.contentSize = CGSizeMake(375 * WIDTH * 4, 667 * HEIGH);
        for (int i = 0; i < 4; i++) {
            UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(375 * WIDTH * i, 0, 375 * WIDTH, 667 * HEIGH)];
            
            myImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d", i + 1]];
            [myScroll addSubview:myImage];
            
        }
        myScroll.pagingEnabled = YES;
        myScroll.showsHorizontalScrollIndicator = NO;
        myScroll.bounces = NO;
        [self.window addSubview:myScroll];
        
        
        /*  最后一个页面设置一个button */
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [myScroll addSubview:button];
        //        button.backgroundColor = [UIColor orangeColor];
        button.frame = CGRectMake(self.window.frame.size.width * 3 + self.window.frame.size.width / 2 - 100 * WIDTH, self.window.frame.size.height - 215 * HEIGH, 200, 100);
//        button.tintColor = [UIColor blackColor];
//        button.layer.borderWidth = 1;
//        button.layer.cornerRadius = 8.0;
        button.titleLabel.font = [UIFont systemFontOfSize:20];
        [button addTarget:self action:@selector(click) forControlEvents:(UIControlEventTouchUpInside)];
        [button setTitle:@"开启新的旅程" forState:UIControlStateNormal];
//        button.backgroundColor = [UIColor colorWithRed:0.622 green:0.711 blue:1.000 alpha:1.000];
        [button setTitleColor:[UIColor colorWithRed:0.441 green:0.629 blue:1.000 alpha:1.000] forState:UIControlStateNormal];
        
        /*  签订协议 */
        myScroll.delegate = self;
        
        /* 页面处理 */
        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 620 * HEIGH, 375*WIDTH, 30)];
        self.page.numberOfPages = 4;
        self.page.currentPage = 0;
        self.page.backgroundColor = [UIColor clearColor];
        self.page.currentPageIndicatorTintColor = [UIColor cyanColor];
        self.page.pageIndicatorTintColor = [UIColor whiteColor];
        [self.window addSubview:self.page];
        
    } else {
        
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
        
        self.window.rootViewController = self.tabbar;
        
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(change:) name:@"change" object:nil];
        
        self.view = [[UIView alloc] initWithFrame:self.window.frame];
        [self.window addSubview:self.view];
        self.view.alpha = 0.5;
        self.view.userInteractionEnabled = NO;
    }

    
    return YES;
}

/* kvo 夜间模式 */
- (void)change:(NSNotification *)notification
{
    self.view.backgroundColor = notification.object;
    
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation{
    
    NSLog(@"Calling Application Bundle ID: %@", sourceApplication);
    NSLog(@"URL scheme:%@", [url scheme]);
    NSLog(@"URL query: %@", [url query]);
    
    
    return YES;
}

/* button的点击方法,处理跳转 */
- (void)click
{
    self.window.rootViewController = self.tabbar;
    
}

/* 页面移动处理 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{
    self.page.currentPage = scrollView.contentOffset.x / scrollView.window.frame.size.width;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
